package activity;

import io.OperationResponse;

import java.util.ArrayList;

import player.Player;
import protocol.ReturnCode;
import MemoryDB.YuanRow;
import define.CommonDefine;

public class ActivityNormal extends ActivityInstance {
	private ArrayList<DefenceInstance> defenceInstanceList = new ArrayList<DefenceInstance>();
	public ActivityNormal(YuanRow activityRow) {
		super(activityRow);

	}

	public void update() {
		for (int i = defenceInstanceList.size() - 1; i >= 0; i--)
		{
			DefenceInstance dfInstance = defenceInstanceList.get(i);
			if(dfInstance.playerList.size() <= 0)
			{
				defenceInstanceList.remove(dfInstance);
				continue;
			}
			dfInstance.update();
		}
	}

	@Override
	public int JoinActivity(Player player, OperationResponse operationRequest) {
		player.log.info("============================Join Activity Normal==========================="	+ this.getActivityRefID());
		int joinReturnType = super.JoinActivity(player, operationRequest);

		if(joinReturnType != CommonDefine.ACTIVITY_JOIN_SUCCESS)
		{
			OperationResponse response ;
			if(joinReturnType == CommonDefine.ACTIVITY_JOIN_COUNT_LIMIT)
			{
				response = new OperationResponse(operationRequest.OperationCode, null, ReturnCode.No, "meg0088" );
			}
			else if(joinReturnType == CommonDefine.ACTIVITY_JOIN_LEVEL_LIMIT)
			{
				response = new OperationResponse(operationRequest.OperationCode, null, ReturnCode.No, "meg0089" );
			}
			else
			{
				response = new OperationResponse(operationRequest.OperationCode, null, ReturnCode.No, "meg0090" );
			}
			player.getSession().SendOperationResponse(response);
			return joinReturnType;
		}

		player.setJoinActivityCount(this.getActivityID());
		DefenceInstance defenceInstance = new DefenceInstance();
		defenceInstance.initDefence(player,this.getActivityRefID(),this.getActivityID());
		this.defenceInstanceList.add(defenceInstance);
				
		/*int JoinActivityCount = player.getJoinActivityCount(this.getActivityID());		
		if (JoinActivityCount == 1)
		{
			sendActivityCanFinish(this.getActivityID(),player.getSession());
			if (!player.getCanFinishActivity().contains(this.getActivityID()))
			{
				player.getCanFinishActivity().add(this.getActivityID());
				player.saveCanFinishActivity();
			}
		}*/
		return CommonDefine.ACTIVITY_JOIN_SUCCESS;
	}
}